<template>
  <div>
    <el-row>
      <el-col :span="20"> 项目列表 </el-col>
      <el-col :span="4" align="right">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleDataInsert('项目选择')"
          >项目选择</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :localPage="true"
        :has-action="true"
      >
        <template #action="{ row, index }">
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; cursor: pointer"

            v-if="row.showDelete"
            @click="delTabelData(index, row)"
          ></i>
        </template>
      </data-table>
    </el-row>
    <el-row>
      <el-col align="end"
        ><el-button type="success" @click="save">确认增项</el-button></el-col
      >
    </el-row>
    <el-dialog
      v-if="visible"
      :title="dialogTitle"
      :visible.sync="visible"
      :append-to-body="true"
      :width="'1000px'"
      @close="dialogClose"
    >
      <SelectProject @getList="changeListCheck" />
    </el-dialog>
  </div>
</template>

<script>
import baseMixin from "@/mixins/base";
import { getDict } from "@/utils";
import { SelectProject } from "@/components/index"; //导入组件库
import { getUserOrTem } from "@/utils/index";

export default {
  name: "carInfo",
  components: { SelectProject },
  props: {
    workOrderId: {
      type: Number | String,
    },
  }, // workOrderType 工单类型 1快速单
  mixins: [
    baseMixin({
      dataName: ["content", "totalRecords"],
      idName: "id",
    }),
  ],
  computed: {},
  data() {
    return {
      configOption: {}, //配置
      fromdata: {
        id: this.workOrderId,
        carProjectLists: [],
      },
      tableColumnAttributes: [
        { name: "项目名", value: "projectName", type: "text" },
        { name: "项目工时", value: "projectHours", type: "text" },
        { name: "项目类型", value: "projectType", type: "text" },
        { name: "项目金额", value: "amount", type: "text" },
        {
          name: "折扣",
          value: "discount",
          type: "inputNumber",
          maxFiled: 0,
          minFiled: 0,
          precision:1,
        },
        {
          name: "折后金额",
          value: "shouldAmount",
          type: "render",
          render: this.discountAmount,
        },
        { name: "状态", value: "status", type: "text" },
        {
          name: "收费类型",
          value: "tollType",
          type: "select",
          options: [
            { index: 1, id: "自费", value: "自费" },
            { index: 2, id: "免费保养", value: "免费保养" },
            { index: 3, id: "索赔", value: "索赔" },
            { index: 4, id: "返工", value: "返工" },
            { index: 5, id: "保险", value: "保险" },
          ],
        },
        { name: "作业人员/组", value: "workUserId", type: "select", options: [] },
      ],
      visible: false,
      tableData: [],
    };
  },
  async created() {
    this.dict = getDict(["返工类型"]);
    this.getParentConfigOption(this.$parent);
    this.searchOptions[
      `${this.configOption.orderReworkList.queryField}`
    ] = this.workOrderId;
    this.query();

    let arry = await getUserOrTem();
    this.workUserList = arry;
    this.tableColumnAttributes[
      this.tableColumnAttributes.length - 1
    ].options = this.workUserList;
  },
  methods: {
    //获取父级配置
    async getParentConfigOption(dom) {
      if ("getConfig" in dom) {
        this.configOption = dom.getConfig;
        return;
      } else if ("$parent" in dom) {
        this.getParentConfigOption(dom.$parent);
      } else {
        return;
      }
    },
    //计算折扣
    discountAmount(row, value) {
      let sum = row.projectHours * row.amount,
        _amount = 0;
      if (row.tollType && (row.tollType == "免单" || row.tollType == "返工")) {
        _amount = 0;
      } else {
        _amount = sum;
        if (row.discount > 0) {
          _amount = sum * Number(row.discount / 10);
        }
      }
      row.shouldAmount = _amount;
      return _amount;
    },
    async query() {
      let f = this.configOption.orderReworkList.query;
      if (f) {
        let res = await f(this.searchOptions);
        this.totalCount = parseInt(res.data["totalRecords"]);
        this.tableData = res.data["content"];
        this.tableData.forEach((e) => {
          e.disabled = true;
        });
      }
    },
    async save() {
      if (this.fromdata.carProjectLists.length === 0) {
        this.$message.error("请选择一条项目数据");
      }
      let f = this.configOption.orderInsProject.save;
      if (f) {
        f(this.fromdata).then((res) => {
          let { success } = res;
          if (success) {
            this.$message.success("操作成功");
            this.$emit("orderInsProjectResult", true);
          } else {
            this.$message.error("系统错误");
          }
        });
      }
    },
    handleDataInsert(title) {
      this.visible = true;
      this.dialogTitle = title;
    },
    //表单删除
    delTabelData(index, row) {
      this.tableData.splice(index, 1);
      this.fromdata.carProjectLists.forEach(e, (i) => {
        if (e.id === row.id) {
          this.fromdata.carProjectLists.splice(i, 1);
        }
      });
    },
    //弹窗选择
    changeListCheck(list, partsList) {
      this.dialogClose();
      list.forEach((item) => {
        item.projectHours = item.workHours;
        item.projectType = item.projectDictValue;
        item.discount = 10;
        item.showDelete = true;
        item.status = "未派工";
        item.carProjectId = item.id;
        item.id = null;
      });
      this.fromdata.carProjectLists = this.fromdata.carProjectLists.concat(list);
      this.tableData = this.tableData.concat(list);
    },
    dialogClose() {
      this.visible = false;
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
</style>
